package com.example.universitylibraryback.event;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class EventPublisher {
    
    private final ApplicationEventPublisher applicationEventPublisher;
    
    @Autowired
    public EventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }
    
    /**
     * 发布新书到馆事件，在业务逻辑代码中的合适位置嵌入事件发布代码（也可以用AOP的切片进行配置实现）
     */
    public void publishNewBookArrival(String bookIsbn, String bookTitle,
                                    String bookAuthor, Integer quantity) {
        NewBookArrivalEvent event = new NewBookArrivalEvent(bookIsbn, bookTitle, bookAuthor, quantity);
        applicationEventPublisher.publishEvent(event);
        System.out.println("发布新书到馆事件: " + bookTitle);
    }
    
    /**
     * 发布借阅到期事件，在业务逻辑代码中的合适位置嵌入事件发布代码（也可以用AOP的切片进行配置实现）
     */
    public void publishBorrowExpiration(String readerId, String readerName, String bookIsbn,
                                       String bookTitle, Date borrowDate, Date dueDate) {
        BorrowExpirationEvent event = new BorrowExpirationEvent(readerId, readerName, bookIsbn,
                                                                bookTitle, borrowDate, dueDate);
        applicationEventPublisher.publishEvent(event);
        System.out.println("发布借阅到期事件: " + readerName + " - " + bookTitle);
    }
    
    /**
     * 发布罚款计算事件，在业务逻辑代码中的合适位置嵌入事件发布代码（也可以用AOP的切片进行配置实现）
     */
    public void publishFineCalculation(String readerId, String borrowRecordId,
                                      Integer overdueDays, String fineReason) {
        FineCalculationEvent event = new FineCalculationEvent(readerId, borrowRecordId,
                                                               overdueDays, fineReason);
        applicationEventPublisher.publishEvent(event);
        System.out.println("发布罚款计算事件: " + readerId + " - 逾期" + overdueDays + "天");
    }
    
    /**
     * 发布热借书籍事件，在业务逻辑代码中的合适位置嵌入事件发布代码（也可以用AOP的切片进行配置实现）
     */
    public void publishHotBook(String bookIsbn, String bookTitle,
                              Integer borrowCount, String hotReason) {
        HotBookEvent event = new HotBookEvent(bookIsbn, bookTitle, borrowCount, hotReason);
        applicationEventPublisher.publishEvent(event);
        System.out.println("发布热借书籍事件: " + bookTitle + " - 借阅次数: " + borrowCount);
    }
    
    /**
     * 发布读者活动事件，在业务逻辑代码中的合适位置嵌入事件发布代码（也可以用AOP的切片进行配置实现）
     */
    public void publishReaderActivity(String activityId, String activityName,
                                     String activityType, Date activityTime, String targetReaders) {
        ReaderActivityEvent event = new ReaderActivityEvent(activityId, activityName,
                                                            activityType, activityTime, targetReaders);
        applicationEventPublisher.publishEvent(event);
        System.out.println("发布读者活动事件: " + activityName);
    }
}